using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace ToolWindows
{
    public partial class Services : UserControl
    {
        private string current_database = string.Empty;
        private string connectionString = null;

        public string CurrentDatabase
        {
            set
            {
                current_database = value;
            }
        }

        public Services()
        {
            InitializeComponent();
        }

        public void RefreshData(string database)
        {
            connectionString = UtilitySqlTools.Current.ConnectionString();

            if (connectionString == null)
            {
                return;
            }

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    StringBuilder sb = new StringBuilder();

                    sb.AppendLine("USE [" + current_database + "];");
                    sb.AppendLine("SELECT   s.name [Name], ");
                    sb.AppendLine("         dp.name [Principal] ");
                    sb.AppendLine("FROM sys.services s ");
                    sb.AppendLine("LEFT JOIN sys.database_principals dp on s.principal_id = dp.principal_id");
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = new SqlCommand();
                    da.SelectCommand.CommandText = sb.ToString();
                    da.SelectCommand.CommandTimeout = 0;
                    da.SelectCommand.CommandType = CommandType.Text;
                    da.SelectCommand.Connection = connection;

                    DataSet ds = new DataSet();
                    da.Fill(ds);

                    dgvServices.DataSource = ds.Tables[0];

                    connection.Close();
                }
            }
            catch (Exception exception)
            {
                // Logger
            }
        }

        private void dgvService_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            string service_name = dgvServices.Rows[e.RowIndex].Cells["Name"].Value.ToString();

            connectionString = UtilitySqlTools.Current.ConnectionString();

            if (connectionString == null)
            {
                return;
            }

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    StringBuilder sb = new StringBuilder();

                    sb.AppendLine("select dp1.name Principal, dp.permission_name Permission, dp.state_desc State " +
                                    "from sys.services s " +
                                    "inner join sys.database_permissions  dp on s.service_id = dp.major_id " +
                                    "inner join sys.database_principals dp1 on dp.grantee_principal_id = dp1.principal_id " +
                                    "where s.name = @name " +
                                    "union all " +
                                    "select dp.name Principal, p.permission_name Permission, 'GRANT' State " +
                                    "from sys.services s " +
                                    "cross apply  sys.fn_builtin_permissions('SERVICE') p " +
                                    "inner join sys.database_principals dp on s.principal_id = dp.principal_id " +
                                    "where s.name = @name");
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = new SqlCommand();
                    da.SelectCommand.CommandText = sb.ToString();
                    da.SelectCommand.CommandTimeout = 0;
                    da.SelectCommand.CommandType = CommandType.Text;
                    da.SelectCommand.Connection = connection;
                    da.SelectCommand.Parameters.AddWithValue("@name", service_name);
                    DataSet ds = new DataSet();
                    da.Fill(ds);

                    dgvPermissions.DataSource = ds.Tables[0];

                    connection.Close();
                }

            }
            catch (Exception exception)
            {
                // Logger
            }
        }
    }
}
